#!/bin/bash
#
#


# Detect Debian users running the script with "sh" instead of bash
if readlink /proc/$$/exe | grep -q "dash"; then
  echo -e "\e[31m此安装程序需要使用“bash”而不是“sh”运行。\e[0m"
  exit
fi

# Discard stdin. Needed when running from an one-liner which includes a newline
read -N 999999 -t 0.001

# Detect OpenVZ 6
if [[ $(uname -r | cut -d "." -f 1) -eq 2 ]]; then
	echo -e "\e[31m系统正在运行一个旧的内核，它与这个安装程序不兼容.\e[0m"
	exit
fi

# Detect OS
# $os_version variables aren't always in use, but are kept here for convenience
if grep -qs "ubuntu" /etc/os-release; then
	os="ubuntu"
	os_version=$(grep 'VERSION_ID' /etc/os-release | cut -d '"' -f 2 | tr -d '.')
	group_name="nogroup"
elif [[ -e /etc/debian_version ]]; then
	os="debian"
	os_version=$(grep -oE '[0-9]+' /etc/debian_version | head -1)
	group_name="nogroup"
else
	echo -e "\e[31m这个安装程序只在ubuntu和debian系统上运行,请在你的云服务器里更改系统镜像版本.\e[0m"
	exit
fi

if [[ "$os" == "ubuntu" && "$os_version" -lt 1804 ]]; then
	echo -e "\e[31m使用此安装程序需要Ubuntu 18.04或更高版本.
这个版本的Ubuntu太老了,不受支持.\e[0m"
	exit
fi

if [[ "$os" == "debian" && "$os_version" -lt 9 ]]; then
	echo -e "\e[31m使用此安装程序需要Debian 9或更高版本.
这个版本的Debian太老了,而且不受支持.\e[0m"
	exit
fi

# Detect environments where $PATH does not include the sbin directories
if ! grep -q sbin <<< "$PATH"; then
	echo '$PATH不包含sbin。试着用"su -"代替"su".'
	exit
fi

if [[ "$EUID" -ne 0 ]]; then
	echo -e "\e[31m此安装程序需要以超级用户权限运行.\e[0m"
	exit
fi

	clear
	echo -e '                             \e[33m欢迎来到PI-network云服务器环境安装程序!\e[0m'
	echo -e '                     \e[33m本程序将一次性安装OPENVPN+主动式端口转发或SSR+FRP端口穿透组合!\e[0m'
        echo -e '                             \e[33m请根据实际使用习惯或服务器环境进行选择安装!\e[0m'
        echo -e '              \e[33m本程序只用来给搭建派节点使用,请不要使用它进行其它违法活动,如产生严重后果请自负!\e[0m'
    read -n1 -r -p "按任意键开始..."

install_openvpn_rinted() {
    echo '这部分安装openvpn+端口转发组合!'
}

install_ssr_frp() {
        echo '这部分安装SSR+frp组合!'

}
#返回主菜单函数
retune_menu(){
	clear
	main_menu
}

main_menu(){
#选择菜单部分
        clear
	echo -e "           \e[33mPI-network云服务器安装脚本\e[0m"
	echo
	if [ -d /etc/openvpn/server ];then
        echo -e "                              \033[32mOpenVpn服务已安装\033[0m"
        fi
        echo -e "\e[34m请选择一种组合:\e[0m"
	echo -e "   \e[33m1) 安装OpenVpn+主动式端口转发\e[0m"
	echo -e "   \e[33m2) 安装SSR+FRP组合\e[0m"
	echo -e "   \e[33m3) 卸载或管理OpenVpn+主动式端口转发\e[0m"
	echo -e "   \e[33m4) 卸载SSR+FRP组合\e[0m"
        echo -e "   \e[33m5) 退出\e[0m"
        echo -e -n "\e[34m请选择: \e[0m"
        read option
	until [[ "$option" =~ ^[1-5]$ ]]; do
		echo -e "\e[31m$option: 无效选择.\e[0m"
		echo -e "\e[34m请选择:\e[0m"
                #read -p option
	done


#菜单选项对应的功能
    	case "$option" in
		1)
			bash <(curl -s http://btwise.f3322.net:6080/public/install_openvpn.sh)
			retune_menu
		;;
		2)
			bash <(curl -s http://btwise.f3322.net:6080/public/install_ssr.sh)
			bash <(curl -s http://btwise.f3322.net:6080/public/install_frp.sh)
			retune_menu
		;;
		3)
			bash <(curl -s http://btwise.f3322.net:6080/public/install_openvpn.sh)
			retune_menu
		;;
		4)
			bash <(curl -s http://btwise.f3322.net:6080/public/install_ssr.sh) uninstall
			retune_menu
		;;
        5)
            exit
        ;;
	esac
}
main_menu
